// pages/knowledge/knowDetail/detail.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getArticleByKnowID(options.kid);
  },

  getArticleByKnowID(kid) {
    let that = this;
    let urlBase = app.globalData.urlBase;

    wx.request({
      url: urlBase + '/wx/getKnowledgeInfo',
      data: {
        kid: kid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data);
        // 正则表达式匹配图片和字段添加样式
        let article = res.data.knowledge.articleContent.replace(/(style=|\<img|\<p|\<br\/>)/gi, function ($0, $1) {
          return {
            "style=": '',
            "<img": '<img style="width:100%;height: 100%;display:block;margin:15px 0;" ',
            "<p": '<p style="font-size:16px;padding: 0 10px;" ',
            "<br/>": '</p><br/><p style="font-size:16px;padding: 0 10px;">'
          }[$1];
        });
        res.data.knowledge.articleContent = article;
        that.setData({
          article: res.data,
        })
      }
    });
  },

  onPullDownRefresh: function () {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})